﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;
using VOL.SmartHealthcare.Infrastructure;
using VOL.Team.DTOS;
using VOL.Team.TeamIservices.TeamManagement.IServices;

namespace VOL.Team.TeamIservices.TeamManagement.Services
{
    public class TeamledgersService : ITeamledgersService
    {
        /// <summary>
        /// 班组仓储
        /// </summary>
        private readonly IBaseRepository<Teamledger> _teamledgerbaseRepository;
        /// <summary>
        /// 每日计划仓储
        /// </summary>
        private readonly IBaseRepository<DailyPlan> _dailyPlanbaseRepository;
        /// <summary>
        /// 工单台账仓储
        /// </summary>
        private readonly IBaseRepository<TicketLedger> _ticketLedgerbaseRepository;
        /// <summary>
        /// 报工仓储
        /// </summary>
        private readonly IBaseRepository<SigningWork> _signingWorkbaseRepository;
        /// <summary>
        /// 工艺路线
        /// </summary>
        private readonly IBaseRepository<Operational> _operationalbaseRepository;
        public TeamledgersService(IBaseRepository<Teamledger> teamledgerbaseRepository, IBaseRepository<DailyPlan> dailyPlanbaseRepository, IBaseRepository<TicketLedger> ticketLedgerbaseRepository, IBaseRepository<SigningWork> signingWorkbaseRepository, IBaseRepository<Operational> operationalbaseRepository)
        {
            _teamledgerbaseRepository = teamledgerbaseRepository;
            _dailyPlanbaseRepository = dailyPlanbaseRepository;
            _ticketLedgerbaseRepository = ticketLedgerbaseRepository;
            _signingWorkbaseRepository = signingWorkbaseRepository;
            _operationalbaseRepository = operationalbaseRepository;
        }
        /// <summary>
        /// 查询班组台账
        /// </summary>
        /// <returns></returns>
        public IQueryable<Teamledger> GetTeamledger()
        {
            try
            {
                var teamledger = _teamledgerbaseRepository.GetAll();
                return teamledger;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 查询当前时刻的班组台账---bpy
        /// </summary>
        /// <returns></returns>
        public IQueryable<DailyPlanTeamledgerDTO> GetNowTeamledger()
        {
            try
            {
                var dailyPlan = _dailyPlanbaseRepository.GetAll();
                var teamledger = _teamledgerbaseRepository.GetAll();

                var dailyPlanTeamledgerDTO = from a in dailyPlan
                                             join b in teamledger on a.Team equals b.TeamName
                                             select new DailyPlanTeamledgerDTO
                                             {
                                                 DailId = a.DailId,
                                                 PlanCode = a.PlanCode,
                                                 OrderCode = a.OrderCode,
                                                 Line = a.Line,
                                                 TargetTime = a.TargetTime,
                                                 Yield = a.Yield,
                                                 Actual = a.Actual,
                                                 Defective = a.Defective,
                                                 PassRate = a.PassRate,
                                                 ActualTake = a.ActualTake,
                                                 Team = b.TeamName,
                                                 MorningshiftStarttim = a.MorningshiftStarttim,
                                                 MorningshiftEndtime = a.MorningshiftEndtime,
                                                 FirstmorningShiftStarttime = a.FirstmorningShiftStarttime,
                                                 FirstmorningShiftEndtime = a.FirstmorningShiftEndtime,
                                                 MiddleShiftStartTime = a.MiddleShiftStartTime,
                                                 MiddleShiftEndTime = a.MiddleShiftEndTime,
                                                 FirstmiddleShiftStartTim = a.FirstmorningShiftStarttime,
                                                 FirstmiddleShiftEndTime = a.FirstmiddleShiftEndTime,
                                                 EveningShiftStartTime = a.EveningShiftStartTime,
                                                 EveningShiftEndTime = a.EveningShiftEndTime,
                                                 FirsteveningShiftStartTime = a.FirsteveningShiftStartTime,
                                                 FirsteveningShiftEndTime = a.FirsteveningShiftEndTime,
                                                 CalibratedTimePerPiece = a.CalibratedTimePerPiece,
                                                 ActualTimePerPiece = a.ActualTimePerPiece,
                                                 TeamId = b.Id,
                                                 Duty = b.Duty,
                                                 Phone = b.Phone,
                                                 TeamNum = b.TeamNum,
                                             };

                return dailyPlanTeamledgerDTO;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取当前生产计划---bpy
        /// </summary>
        /// <returns></returns>
        public IQueryable<TicketLedger> GetTicketLedger()
        {
            try
            {
                var ticketLedger = _ticketLedgerbaseRepository.GetAll();
                return ticketLedger;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取报工信息---bpy
        /// </summary>
        /// <returns></returns>
        public IQueryable<SigningWork> GetSigningWork()
        {
            try
            {
                var signingWork = _signingWorkbaseRepository.GetAll();
                return signingWork;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 工单台账和工艺路线
        /// </summary>
        /// <returns></returns>
        public IQueryable<TicketLedgerOperationalDTO> GetTicketLedgerOperational()
        {
            try
            {
                //工单台账
                var TicketLedger = _ticketLedgerbaseRepository.GetAll();
                //工艺路线
                var operational = _operationalbaseRepository.GetAll();
                //数据对象
                var ticketLedgerOperationalDTO = from a in TicketLedger
                                                 join b in operational on a.Line equals Convert.ToString(b.Id)
                                                 select new TicketLedgerOperationalDTO
                                                 {
                                                     TickeId = a.TickeId,
                                                     TickeNumber = a.TickeNumber,
                                                     Line = b.Routing,
                                                     Plan = a.Plan,
                                                     StateTime = a.StateTime,
                                                     EndTime = a.EndTime,
                                                     Bomcode = a.Bomcode,
                                                     QRCode = a.QRCode,
                                                     Produce = a.Produce,
                                                     DefectiveProduct = a.DefectiveProduct,
                                                     FirstPassRate = a.FirstPassRate,
                                                     OrderFulfillmentRate = a.OrderFulfillmentRate,
                                                     Remark = a.Remark,
                                                     OrdersState = a.OrdersState,
                                                     RoutingId = b.Id,
                                                     ProcessDescription = b.ProcessDescription,
                                                     ProcessTiming = b.ProcessTiming
                                                 };
                return ticketLedgerOperationalDTO;
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}
